package com.ks.core.file.oss;


import com.aliyun.oss.OSSClient;
import com.aliyun.oss.model.ObjectMetadata;
import org.springframework.web.multipart.MultipartFile;

import java.io.ByteArrayInputStream;
import java.io.InputStream;

/**
 * 对OSS服务器进行上传删除等的处理
 *
 * @ClassName: OSSManageUtil
 * @Description:
 */
public class
OSSManageUtil {

    private static OSSConfigure ossConfigure;

    public static void initConfig() {
    }

    /**
     * 上传OSS服务器文件
     *
     * @param @param  ossConfigure
     * @param @param  file
     * @param @param  remotePath
     * @param @return
     * @param @throws Exception 设定文件
     * @return String 返回类型
     * @throws
     * @Title: uploadFile
     * @Description:
     */
    public static String uploadFile(MultipartFile file, String remotePath, String fileName)
            throws Exception {
      /*  InputStream fileContent = null;
        fileContent = new FileInputStream(file);*/

        if (ossConfigure == null) {
            ossConfigure =
           /* new OSSConfigure("oss-cn-shanghai.aliyuncs.com", "LTAI141fs0mfKxgT", "6nK7U0TnNQ9hbUvb0D5J9Vdtw8NCgF",
                    "yunhongoss", "https://yunhongoss.oss-cn-shanghai.aliyuncs.com");*/
                    new OSSConfigure("oss-cn-beijing.aliyuncs.com", "LTAI4G1vCzBEj1vQyAziGxLY", "HsuvT3OkTy0ZWmKrE2emzmR2IZGpdD",
                            "yuhongoss", "https://yuhongoss.oss-cn-beijing.aliyuncs.com");
        }
        try {

            OSSClient ossClient =
                    new OSSClient(ossConfigure.getEndpoint(), ossConfigure.getAccessKeyId(), ossConfigure.getAccessKeySecret());
            String remoteFilePath = remotePath;

            // 创建上传Object的Metadata
            ObjectMetadata objectMetadata = new ObjectMetadata();
            /*	objectMetadata.setContentLength(fileContent.available());*/
            objectMetadata.setCacheControl("no-cache");
            objectMetadata.setHeader("Pragma", "no-cache");
            objectMetadata.setContentType(contentType(fileName.substring(fileName.lastIndexOf("."))));
            objectMetadata.setContentDisposition("inline;filename=" + fileName);
            // 上传文件
            InputStream is = new ByteArrayInputStream(file.getBytes());
            //ossClient.putObject(ossConfigure.getBucketName(), remoteFilePath + fileName, fileContent, objectMetadata);
            ossClient.putObject(ossConfigure.getBucketName(), remoteFilePath + fileName, is);
            System.out.println(ossConfigure.getAccessUrl() + "/" + remoteFilePath + fileName);
            return ossConfigure.getAccessUrl() + "/" + remoteFilePath + fileName;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }


  /*  public static String uploadFile1(MultipartFile file, String remotePath, String fileName) {

        OSS ossClient = new OSSClientBuilder().build("oss-cn-beijing.aliyuncs.com", "LTAI4G1vCzBEj1vQyAziGxLY", "HsuvT3OkTy0ZWmKrE2emzmR2IZGpdD");

        PutObjectRequest putObjectRequest = new PutObjectRequest("yuhongoss", remotePath + fileName, new File(file));

        ossClient.putObject(putObjectRequest);

// 关闭OSSClient。
        ossClient.shutdown();
    }*/

    /**
     * 根据key删除OSS服务器上的文件
     *
     * @param @param ossConfigure
     * @param @param filePath 设定文件
     * @return void 返回类型
     * @throws
     * @Title: deleteFile
     * @Description:
     */
    public static void deleteFile(OSSConfigure ossConfigure, String filePath) {
        OSSClient ossClient =
                new OSSClient(ossConfigure.getEndpoint(), ossConfigure.getAccessKeyId(), ossConfigure.getAccessKeySecret());
        ossClient.deleteObject(ossConfigure.getBucketName(), filePath);

    }

    /**
     * Description: 判断OSS服务文件上传时文件的contentType
     *
     * @param FilenameExtension 文件后缀
     * @return String
     * @Version1.0
     */
    public static String contentType(String FilenameExtension) {
        if (FilenameExtension.equals("BMP") || FilenameExtension.equals("bmp")) {
            return "image/bmp";
        }
        if (FilenameExtension.equals("GIF") || FilenameExtension.equals("gif")) {
            return "image/gif";
        }
        if (FilenameExtension.equals("JPEG") || FilenameExtension.equals("jpeg") || FilenameExtension.equals("JPG")
                || FilenameExtension.equals("jpg") || FilenameExtension.equals("PNG") || FilenameExtension.equals("png")) {
            return "image/jpg";
        }
        if (FilenameExtension.equals("HTML") || FilenameExtension.equals("html")) {
            return "text/html";
        }
        if (FilenameExtension.equals("TXT") || FilenameExtension.equals("txt")) {
            return "text/plain";
        }
        if (FilenameExtension.equals("VSD") || FilenameExtension.equals("vsd")) {
            return "application/vnd.visio";
        }
        if (FilenameExtension.equals("PPTX") || FilenameExtension.equals("pptx") || FilenameExtension.equals("PPT")
                || FilenameExtension.equals("ppt")) {
            return "application/vnd.ms-powerpoint";
        }
        if (FilenameExtension.equals("DOCX") || FilenameExtension.equals("docx") || FilenameExtension.equals("DOC")
                || FilenameExtension.equals("doc")) {
            return "application/msword";
        }
        if (FilenameExtension.equals("XML") || FilenameExtension.equals("xml")) {
            return "text/xml";
        }
        return "application/octet-stream";
    }
}
